package com.tom.architect02.zframework.sreenAdapter.linearpercent;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import com.tom.architect02.R;

/**
 * Added by Tom on 2024/04/11.
 *
 * <?xml version="1.0" encoding="utf-8"?>
 * <com.maniu.linearlayoutpercent.LinearLayoutPercent xmlns:android="http://schemas.android.com/apk/res/android"
 *     xmlns:app="http://schemas.android.com/apk/res-auto"
 *     xmlns:tools="http://schemas.android.com/tools"
 *     android:layout_width="match_parent"
 *     android:layout_height="match_parent"
 *     tools:context=".MainActivity">
 *     <TextView
 *         android:id="@+id/tv"
 *         android:layout_width="wrap_content"
 *         android:layout_height="wrap_content"
 *         android:text="Hello World!"
 *         android:background="#ff0000"
 *         app:layout_leftMarginPercent="0.1"
 *         app:layout_widthPercent="0.2"
 *         app:layout_heightPercent="1"/>
 *     <TextView
 *         android:layout_width="wrap_content"
 *         android:layout_height="wrap_content"
 *         android:text="Hello World!"
 *         android:background="#00ff00"
 *         app:layout_widthPercent="0.6"
 *         app:layout_rightMarginPercent="0.1"
 *         app:layout_heightPercent="1"/>
 * </com.maniu.linearlayoutpercent.LinearLayoutPercent>
 */
public class LinearLayoutPercent extends LinearLayout {

    public LinearLayoutPercent(Context context) {
        super(context);
    }

    public LinearLayoutPercent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LinearLayoutPercent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//      测量  当前父容器 的大小
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {

            // 遍历子控件
            View child = getChildAt(i);
            ViewGroup.LayoutParams params = child.getLayoutParams();
            if (params instanceof DavidLayoutParams) {
                DavidLayoutParams lp = (DavidLayoutParams) params;
                float widthPercent = lp.widthPercent; // 读取xml配置的百分比值
                float heightPercent = lp.heightPercent;
                float leftMarginPercent = lp.leftMarginPercent;
                float rightMarginPercent = lp.rightMarginPercent;
                float topMarginPercent = lp.topMarginPercent;
                float bottomMarginPercent = lp.bottomMarginPercent;
                if (widthPercent > 0) {
                    // 宽度 = 父容器的宽度* 宽度百分比。
                    params.width = (int) (widthSize * widthPercent);
                }
                if (heightPercent > 0) {
                    params.height = (int) (heightSize * heightPercent);
                }
                if (leftMarginPercent > 0) {
                    ((LayoutParams) params).leftMargin = (int) (widthSize * leftMarginPercent);
                }
                if (rightMarginPercent > 0) {
                    ((LayoutParams) params).rightMargin = (int) (widthSize * rightMarginPercent);
                }
                if (topMarginPercent > 0) {
                    ((LayoutParams) params).topMargin = (int) (heightSize * topMarginPercent);
                }
                if (bottomMarginPercent > 0) {
                    ((LayoutParams) params).bottomMargin = (int) (heightSize * bottomMarginPercent);
                }
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

//    性能角度：  代码适配》 百分比 》 头条适配  越前性能越高
//    开发角度：  代码适配复杂 》百分比 >头条适配
// 容器在添加View    的时候         容器的LayoutParams《---- generateLayoutParams
// TextView   --->   LayoutParams  《-----  generateLayoutParams（）
    @Override
    public LinearLayout.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new DavidLayoutParams(getContext(), attrs);
    }

    public static class DavidLayoutParams extends LinearLayout.LayoutParams {
        private float widthPercent;
        private float heightPercent;
        private float leftMarginPercent;
        private float rightMarginPercent;
        private float topMarginPercent;
        private float bottomMarginPercent;

        public DavidLayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
//            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.LinearLayoutPercent);
//            widthPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_widthPercent, 0f);
//            heightPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_heightPercent, 0f);
//            leftMarginPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_leftMarginPercent, 0f);
//            rightMarginPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_rightMarginPercent, 0f);
//            topMarginPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_topMarginPercent, 0f);
//            bottomMarginPercent = a.getFloat(R.styleable.LinearLayoutPercent_layout_bottomMarginPercent, 0f);
//            a.recycle();
        }
    }

}
